package com.rcadmin.busi.web.action;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.rcadmin.busi.web.ddl.WebImage;
import com.rcadmin.core.app.action.ResponseData;
import com.rcadmin.core.app.action.ResponseUtils;
import com.rcadmin.core.web.BaseAction;

/**
 * 内容管理接口服务
 * 
 * @author tanson lam
 * @create 2016年10月2日
 */
@RestController
public class ImageRest extends BaseAction {

	/**
	 * 保存
	 */
	@RequestMapping(value = "/image/save.op", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData save(@RequestBody WebImage webImage) {
		ResponseData responseData = null;
		String failMes = "保存失败！";
		try {
			webImage.setUserId(getLoginUserId());
			if (webImage.getId() != null) {
				baseModelService.update(webImage);
			} else {
				baseModelService.save(webImage);
			}
			responseData = ResponseUtils.success("保存成功！");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			responseData = ResponseUtils.fail(failMes);
		}
		return responseData;
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/image/delete.op", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData delete(String selectItems) {
		ResponseData responseData = null;
		try {
			if (StringUtils.isEmpty(selectItems)) {
				responseData = ResponseUtils.fail("没有选中的数据！");
				return responseData;
			}
			String[] selectArr = selectItems.split(",");
			for (String id : selectArr) {
				WebImage webImage = baseModelService.findOne(
						Integer.valueOf(id), WebImage.class);
				if (webImage != null)
					baseModelService.delete(webImage);
			}
			responseData = ResponseUtils.success("删除成功！");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			responseData = ResponseUtils.fail("删除失败！");
		}
		return responseData;
	}
}
